/*   1:    */ package org.intellij.plugins.relaxNG.inspections;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.PsiElementProcessor.CollectElements;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.Query;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomManager;
import org.intellij.plugins.relaxNG.compact.psi.RncDefine;
import org.intellij.plugins.relaxNG.compact.psi.RncElementVisitor;
import org.intellij.plugins.relaxNG.compact.psi.RncGrammar;
import org.intellij.plugins.relaxNG.compact.psi.impl.RncDefineImpl;
import org.intellij.plugins.relaxNG.model.resolve.RelaxIncludeIndex;
import org.intellij.plugins.relaxNG.xml.dom.RngGrammar;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */ 
/*  37:    */ public class UnusedDefineInspection
/*  38:    */   extends BaseInspection
/*  39:    */ {
/*  40:    */   public boolean isEnabledByDefault()
/*  41:    */   {
/*  42: 61 */     return false;
/*  43:    */   }
/*  44:    */   
/*  45:    */   @Nls
/*  46:    */   @NotNull
/*  47:    */   public String getDisplayName()
/*  48:    */   {
/*  49: 67 */     String tmp2_0 = "Unused Define";
/*  50: 67 */     if (tmp2_0 == null) {
/*  51: 67 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection", "getDisplayName" }));
/*  52:    */     }
/*  53: 67 */     return tmp2_0;
/*  54:    */   }
/*  55:    */   
/*  56:    */   @NonNls
/*  57:    */   @NotNull
/*  58:    */   public String getShortName()
/*  59:    */   {
/*  60: 73 */     String tmp2_0 = "UnusedDefine";
/*  61: 73 */     if (tmp2_0 == null) {
/*  62: 73 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection", "getShortName" }));
/*  63:    */     }
/*  64: 73 */     return tmp2_0;
/*  65:    */   }
/*  66:    */   
/*  67:    */   @NotNull
/*  68:    */   public RncElementVisitor buildVisitor(@NotNull ProblemsHolder paramProblemsHolder, boolean paramBoolean)
/*  69:    */   {
/*  70: 78 */     if (paramProblemsHolder == null) {
/*  71: 78 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection", "buildVisitor" }));
/*  72:    */     }
/*  73: 78 */     void tmp48_45 = new MyElementVisitor(paramProblemsHolder);
/*  74: 78 */     if (tmp48_45 == null) {
/*  75: 78 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection", "buildVisitor" }));
/*  76:    */     }
/*  77: 78 */     return tmp48_45;
/*  78:    */   }
/*  79:    */   
/*  80:    */   private static final class MyElementVisitor
/*  81:    */     extends RncElementVisitor
/*  82:    */   {
/*  83:    */     private final ProblemsHolder a;
/*  84: 84 */     private final XmlElementVisitor b = new XmlElementVisitor()
/*  85:    */     {
/*  86:    */       public void visitXmlTag(XmlTag paramAnonymousXmlTag)
/*  87:    */       {
/*  88: 87 */         UnusedDefineInspection.MyElementVisitor.this.visitXmlTag(paramAnonymousXmlTag);
/*  89:    */       }
/*  90:    */     };
/*  91:    */     
/*  92:    */     public MyElementVisitor(ProblemsHolder paramProblemsHolder)
/*  93:    */     {
/*  94: 92 */       this.a = paramProblemsHolder;
/*  95:    */     }
/*  96:    */     
/*  97:    */     protected void superVisitElement(PsiElement paramPsiElement)
/*  98:    */     {
/*  99: 96 */       paramPsiElement.accept(this.b);
/* 100:    */     }
/* 101:    */     
/* 102:    */     public void visitDefine(RncDefine paramRncDefine)
/* 103:    */     {
/* 104:101 */       RncGrammar localRncGrammar = (RncGrammar)PsiTreeUtil.getParentOfType(paramRncDefine, RncGrammar.class);
/* 105:102 */       PsiFile localPsiFile = paramRncDefine.getContainingFile();
/* 106:103 */       if (localRncGrammar != null)
/* 107:    */       {
/* 108:104 */         if (!a(paramRncDefine, new LocalSearchScope(localRncGrammar))) {}
/* 109:    */       }
/* 110:106 */       else if (a(paramRncDefine, new LocalSearchScope(localPsiFile))) {
/* 111:106 */         return;
/* 112:    */       }
/* 113:109 */       PsiElementProcessor.CollectElements localCollectElements = new PsiElementProcessor.CollectElements();
/* 114:110 */       RelaxIncludeIndex.processBackwardDependencies((XmlFile)localPsiFile, localCollectElements);
/* 115:112 */       if (a(paramRncDefine, new LocalSearchScope(localCollectElements.toArray()))) {
/* 116:112 */         return;
/* 117:    */       }
/* 118:114 */       ASTNode localASTNode = ((RncDefineImpl)paramRncDefine).getNameNode();
/* 119:115 */       this.a.registerProblem(localASTNode.getPsi(), "Unreferenced define", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new LocalQuickFix[] { new MyFix(paramRncDefine) });
/* 120:    */     }
/* 121:    */     
/* 122:    */     private static boolean a(PsiElement paramPsiElement, LocalSearchScope paramLocalSearchScope)
/* 123:    */     {
/* 124:119 */       Query localQuery = ReferencesSearch.search(paramPsiElement, paramLocalSearchScope);
/* 125:120 */       for (PsiReference localPsiReference : localQuery)
/* 126:    */       {
/* 127:121 */         PsiElement localPsiElement = localPsiReference.getElement();
/* 128:122 */         RncDefine localRncDefine = (RncDefine)PsiTreeUtil.getParentOfType(localPsiElement, RncDefine.class, false);
/* 129:123 */         if ((localRncDefine == null) || (!PsiTreeUtil.isAncestor(paramPsiElement, localRncDefine, true))) {
/* 130:124 */           return true;
/* 131:    */         }
/* 132:    */       }
/* 133:127 */       return false;
/* 134:    */     }
/* 135:    */     
/* 136:    */     public void visitXmlTag(XmlTag paramXmlTag)
/* 137:    */     {
/* 138:131 */       PsiFile localPsiFile = paramXmlTag.getContainingFile();
/* 139:132 */       if (localPsiFile.getFileType() != StdFileTypes.XML) {
/* 140:133 */         return;
/* 141:    */       }
/* 142:135 */       if (!paramXmlTag.getLocalName().equals("define")) {
/* 143:136 */         return;
/* 144:    */       }
/* 145:138 */       if (!paramXmlTag.getNamespace().equals("http://relaxng.org/ns/structure/1.0")) {
/* 146:139 */         return;
/* 147:    */       }
/* 148:141 */       if (paramXmlTag.getAttribute("combine") != null) {
/* 149:142 */         return;
/* 150:    */       }
/* 151:145 */       XmlAttribute localXmlAttribute = paramXmlTag.getAttribute("name");
/* 152:146 */       if (localXmlAttribute == null) {
/* 153:146 */         return;
/* 154:    */       }
/* 155:148 */       XmlAttributeValue localXmlAttributeValue = localXmlAttribute.getValueElement();
/* 156:149 */       if (localXmlAttributeValue == null) {
/* 157:149 */         return;
/* 158:    */       }
/* 159:151 */       String str = localXmlAttributeValue.getValue();
/* 160:152 */       if ((str == null) || (str.length() == 0)) {
/* 161:153 */         return;
/* 162:    */       }
/* 163:155 */       PsiElement localPsiElement1 = localXmlAttributeValue.getParent();
/* 164:156 */       if (!(localPsiElement1 instanceof XmlAttribute)) {
/* 165:157 */         return;
/* 166:    */       }
/* 167:159 */       if (!"name".equals(((XmlAttribute)localPsiElement1).getName())) {
/* 168:160 */         return;
/* 169:    */       }
/* 170:162 */       PsiElement localPsiElement2 = localPsiElement1.getParent();
/* 171:163 */       if (!(localPsiElement2 instanceof XmlTag)) {
/* 172:164 */         return;
/* 173:    */       }
/* 174:167 */       DomElement localDomElement = DomManager.getDomManager(paramXmlTag.getProject()).getDomElement(paramXmlTag);
/* 175:168 */       if (localDomElement == null) {
/* 176:169 */         return;
/* 177:    */       }
/* 178:172 */       RngGrammar localRngGrammar = (RngGrammar)localDomElement.getParentOfType(RngGrammar.class, true);
/* 179:173 */       if (localRngGrammar != null)
/* 180:    */       {
/* 181:174 */         if (!a(paramXmlTag, localXmlAttributeValue, new LocalSearchScope(localRngGrammar.getXmlTag()))) {}
/* 182:    */       }
/* 183:176 */       else if (a(paramXmlTag, localXmlAttributeValue, new LocalSearchScope(localPsiFile))) {
/* 184:176 */         return;
/* 185:    */       }
/* 186:179 */       PsiElementProcessor.CollectElements localCollectElements = new PsiElementProcessor.CollectElements();
/* 187:180 */       RelaxIncludeIndex.processBackwardDependencies((XmlFile)localPsiFile, localCollectElements);
/* 188:182 */       if (a(paramXmlTag, localXmlAttributeValue, new LocalSearchScope(localCollectElements.toArray()))) {
/* 189:182 */         return;
/* 190:    */       }
/* 191:184 */       this.a.registerProblem(localXmlAttributeValue, "Unreferenced define", ProblemHighlightType.LIKE_UNUSED_SYMBOL, new LocalQuickFix[] { new MyFix(paramXmlTag) });
/* 192:    */     }
/* 193:    */     
/* 194:    */     private static boolean a(PsiElement paramPsiElement, XmlAttributeValue paramXmlAttributeValue, LocalSearchScope paramLocalSearchScope)
/* 195:    */     {
/* 196:188 */       Query localQuery = ReferencesSearch.search(paramPsiElement, paramLocalSearchScope, true);
/* 197:189 */       for (PsiReference localPsiReference : localQuery)
/* 198:    */       {
/* 199:190 */         PsiElement localPsiElement = localPsiReference.getElement();
/* 200:191 */         if (localPsiElement != paramXmlAttributeValue)
/* 201:    */         {
/* 202:192 */           XmlTag localXmlTag = (XmlTag)PsiTreeUtil.getParentOfType(localPsiElement, XmlTag.class);
/* 203:193 */           if ((localXmlTag != null) && (!PsiTreeUtil.isAncestor(paramPsiElement, localXmlTag, true))) {
/* 204:194 */             return true;
/* 205:    */           }
/* 206:    */         }
/* 207:    */       }
/* 208:198 */       return false;
/* 209:    */     }
/* 210:    */     
/* 211:    */     private static class MyFix<T extends PsiElement>
/* 212:    */       implements LocalQuickFix
/* 213:    */     {
/* 214:    */       private final T a;
/* 215:    */       
/* 216:    */       public MyFix(T paramT)
/* 217:    */       {
/* 218:205 */         this.a = paramT;
/* 219:    */       }
/* 220:    */       
/* 221:    */       @NotNull
/* 222:    */       public String getName()
/* 223:    */       {
/* 224:210 */         String tmp2_0 = "Remove Define";
/* 225:210 */         if (tmp2_0 == null) {
/* 226:210 */           throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection$MyElementVisitor$MyFix", "getName" }));
/* 227:    */         }
/* 228:210 */         return tmp2_0;
/* 229:    */       }
/* 230:    */       
/* 231:    */       @NotNull
/* 232:    */       public String getFamilyName()
/* 233:    */       {
/* 234:215 */         String tmp4_1 = getName();
/* 235:215 */         if (tmp4_1 == null) {
/* 236:215 */           throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection$MyElementVisitor$MyFix", "getFamilyName" }));
/* 237:    */         }
/* 238:215 */         return tmp4_1;
/* 239:    */       }
/* 240:    */       
/* 241:    */       public void applyFix(@NotNull Project paramProject, @NotNull ProblemDescriptor paramProblemDescriptor)
/* 242:    */       {
/* 243:220 */         if (paramProject == null) {
/* 244:220 */           throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection$MyElementVisitor$MyFix", "applyFix" }));
/* 245:    */         }
/* 246:220 */         if (paramProblemDescriptor == null) {
/* 247:220 */           throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "1", "org/intellij/plugins/relaxNG/inspections/UnusedDefineInspection$MyElementVisitor$MyFix", "applyFix" }));
/* 248:    */         }
/* 249:    */         try
/* 250:    */         {
/* 251:220 */           if (this.a.isValid()) {
/* 252:221 */             this.a.delete();
/* 253:    */           }
/* 254:    */         }
/* 255:    */         catch (IncorrectOperationException localIncorrectOperationException)
/* 256:    */         {
/* 257:224 */           Logger.getInstance(UnusedDefineInspection.class.getName()).error(localIncorrectOperationException);
/* 258:    */         }
/* 259:    */       }
/* 260:    */     }
/* 261:    */   }
/* 262:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.inspections.UnusedDefineInspection

 * JD-Core Version:    0.7.0.1

 */